package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.domain.Paymen;
import cn.wolfcode.crm.domain.Staging;
import cn.wolfcode.crm.mapper.ProductMapper;
import cn.wolfcode.crm.mapper.PaymenMapper;
import cn.wolfcode.crm.mapper.StagingMapper;
import cn.wolfcode.crm.query.StagingQueryObject;
import cn.wolfcode.crm.service.IStagingService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class StagingServiceImpl implements IStagingService {

    @Autowired
    StagingMapper stagingMapper;
    @Autowired
    private PaymenMapper paymenMapper;

    @Autowired
    ProductMapper productMapper;

    public PageResult query(StagingQueryObject qo) {
        int count = stagingMapper.query4Count(qo);
        if (count > 0) {
            return new PageResult(count, stagingMapper.query4List(qo));
        }
        return new PageResult(count, Collections.EMPTY_LIST);
    }

    public int deleteByPrimaryKey(Long id) {return stagingMapper.deleteByPrimaryKey(id);
    }

    public int insert(Staging record,Long[] pIds) {
        // 1. 获取当前登录用户,设置录入人相关信息
        Employee currentUser = UserContext.getCurrentUser();
        record.setInputUser(currentUser);
        record.setApplyDate(new Date());
        record.setState(0L);


        String result="XXX";
        for (int i=0;i<16;i++)
        {
            result+=new Random().nextInt(10);
        }

        record.setSn(result);

        for (Long aLong : pIds) {
            Product pt = productMapper.selectByPrimaryKey(aLong);
            record.setProduct(pt);

        }

        return stagingMapper.insert(record);
    }

    public Staging selectByPrimaryKey(Long id) {
        return stagingMapper.selectByPrimaryKey(id);
    }

    public List<Staging> selectAll() {
        return stagingMapper.selectAll();
    }

    public int updateByPrimaryKey(Staging record) {
        return stagingMapper.updateByPrimaryKey(record);
    }

    public void drop(Long id) {
        stagingMapper.drop(id);
    }

    //审核完提交到缴费处理
    public void remand(Long id) {
        stagingMapper.remand(id);
    }

    public void back(Long id,String opinion) {
        stagingMapper.back(id,opinion);
    }

    public void through(Long id,Staging record) {
        Staging staging = stagingMapper.selectByPrimaryKey(id);
        Paymen paymen = new Paymen();
        paymen.setClient(staging.getCustomer());
        paymen.setSn(staging.getSn());
        paymen.setMoney(staging.getTotalAmout());
        paymen.setDuration(staging.getDuration());
        paymen.setCheckDate(new Date());
        paymen.setOrganization(staging.getInstitutions());
        paymen.setProduct(staging.getProduct());
        paymen.setStatus(false);
        paymenMapper.insert(paymen);

        Employee currentUser = UserContext.getCurrentUser();
        record.setAuditor(currentUser);
        stagingMapper.through(id,record);
    }

    public Staging echo(Long id) {
        return stagingMapper.echo(id);
    }
}
